/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ChatServerImpl;
import controller.ClientInt;
import java.rmi.registry.Registry;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author mona
 */
public class ControlPanel extends javax.swing.JFrame {

    Registry reg ;
    ChatServerImpl server;

    public ControlPanel(ChatServerImpl obj, Registry r) {
        initComponents();
        server = obj;
        reg=r;
        jLabel4.setText(Integer.toString(server.countOnlineUsers()));
        jLabel5.setText(Integer.toString(server.countOfflineUsers()));

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(650, 650));
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 24)); // NOI18N
        jLabel1.setText("SERVER CONTROL PANEL");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(130, 10, 350, 50);

        jToggleButton1.setText("STOP");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jToggleButton1);
        jToggleButton1.setBounds(221, 72, 170, 50);

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 18)); // NOI18N
        jLabel2.setText("Online Users :");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(21, 238, 148, 22);

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 18)); // NOI18N
        jLabel3.setText("Offline Users :");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(21, 319, 146, 22);

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 18)); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(232, 238, 136, 25);

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 1, 18)); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(232, 319, 88, 22);

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 18)); // NOI18N
        jLabel6.setText("Advertise :");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(21, 399, 148, 22);
        getContentPane().add(jTextField1);
        jTextField1.setBounds(21, 440, 397, 60);

        jButton1.setText("GO ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(445, 439, 80, 60);

        jButton2.setText("Add User");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(40, 360, 100, 29);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        try {

            if (jToggleButton1.getText().equals("STOP")) {
                jToggleButton1.setText("START");

                reg.unbind("HelloService");
                server.shutDownUsers();
                updateLabels();

            } else {
                jToggleButton1.setText("STOP");
                reg.rebind("HelloService", server);           
            }

        } catch (Exception ex) {
            System.out.println("Problem in Start/Stop Button");
        }

    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {

            String messagee = jTextField1.getText();

            server.sendAds(messagee);

        } catch (Exception ex) {
            System.out.println("Problem in Sending Ads");
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        
            SignUp add=new SignUp();
            add.setVisible(rootPaneCheckingEnabled);
          
        
        
    }//GEN-LAST:event_jButton2ActionPerformed

    public void updateLabels() {
        jLabel4.setText(Integer.toString(server.countOnlineUsers()));
        jLabel5.setText(Integer.toString(server.countOfflineUsers()));

    }
    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private static javax.swing.JLabel jLabel4;
    private static javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables
}
